package com.qingyu.print.order.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.mysql.cj.x.protobuf.Mysqlx;
import com.qingyu.print.order.entity.OrderEntity;
import com.qingyu.print.order.service.OrderService;
import com.qingyu.print.order.utils.OrderNumUtil;
import com.qingyu.print.order.vo.OrderVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.qingyu.print.order.entity.FileEntity;
import com.qingyu.print.order.service.FileService;
import com.qingyu.common.utils.PageUtils;
import com.qingyu.common.utils.R;



/**
 * 
 *
 * @author qingyu
 * @email 1548686075@qq.com
 * @date 2021-08-15 15:22:49
 */
@RestController
@RequestMapping("order/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/test")
    public String get(){
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",new OrderEntity());
        return "ok";
    }

    @PostMapping("/save")
    public R save(@RequestBody OrderVo orderVo){

        FileEntity file = new FileEntity();
        file.setName(orderVo.getName());
        file.setCopies(orderVo.getCopies());
        file.setPrices(orderVo.getPrices());
        file.setType(orderVo.getType());
        file.setPage(orderVo.getPage());
        fileService.save(file);

        OrderEntity order=new OrderEntity();
        order.setFile(file.getName());
        order.setStatus(0);
        order.setCreatAt(OrderNumUtil.getTime());
        order.setId(OrderNumUtil.getOrderNum());
        order.setUid(orderVo.getUid());
        orderService.save(order);

        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",order);

        System.out.println(file.toString());
        System.out.println(order.toString());

        /**
         * 文件上传后应该生成订单-->返回订单数据给前端--->前端收到数据进行显示
         */
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("order:file:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = fileService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{name}")
    //@RequiresPermissions("order:file:info")
    public R info(@PathVariable("name") String name){
		FileEntity file = fileService.getById(name);

        return R.ok().put("file", file);
    }

    /**
     * 保存
     */
//    @RequestMapping("/save")
    //@RequiresPermissions("order:file:save")
//    @PostMapping("/save")
    public R savea(@RequestBody FileEntity file){
		fileService.save(file);

        System.out.println(file.toString());
//        order.setFile(file.getName());
//        System.out.println(order);
        /**
         * 文件上传后应该生成订单-->返回订单数据给前端--->前端收到数据进行显示
         */
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   //@RequiresPermissions("order:file:update")
    public R update(@RequestBody FileEntity file){
		fileService.updateById(file);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("order:file:delete")
    public R delete(@RequestBody String[] names){
		fileService.removeByIds(Arrays.asList(names));

        return R.ok();
    }

}
